#! /bin/bash
echo "--------------begin install redis--------------"

basepath="/usr/local"
installpath=$(cd `dirname $0`; pwd)
redisname="redis-5.0.3"

# install c++ evn
yum install -y gcc

# install redis
wget http://download.redis.io/releases/${redisname}.tar.gz
tar -zxvf ${redisname}.tar.gz
cd ${installpath}
cd ${redisname}
make
make install PREFIX=${basepath}/${redisname}

# set redis-server run daemon
cp redis.conf ${basepath}/${redisname}/bin/
cd ${basepath}/${redisname}/bin/
sed -i 's/daemonize no/daemonize yes/g' redis.conf

# set redis-service auto start when reboot
touch /etc/systemd/system/redis.service
echo "[Unit]
Description=redis-server
After=network.target

[Service]
Type=forking
ExecStart=${basepath}/redis/bin/redis-server ${basepath}/redis/bin/redis.conf
PrivateTmp=true

[Install]
WantedBy=multi-user.target" >> /etc/systemd/system/redis.service
systemctl daemon-reload
systemctl start redis-test.service
systemctl enable redis-test.service

# create soft link for redis
ln -s ${basepath}/${redisname}/bin/redis-cli /usr/bin/redis

echo "--------------install success!------------"